//
//  SettingTableViewController.m
//  JiongTu
//
//  Created by 杨璟 on 16/3/17.
//  Copyright © 2016年 YSKJ. All rights reserved.
//

#import "SettingTableViewController.h"

#import "InstructionViewController.h"
#import "AboutUsViewController.h"
#import "JTProvisionViewController.h"

#import "SettingCell.h"
#import "JTBaseNaviController.h"

#import "JTUser.h"
#import <AVOSCloud.h>
#import <SVProgressHUD.h>
#import <LeanCloudFeedback.h>

#import "SDWebImageManager.h"

@interface SettingTableViewController ()<UITableViewDataSource, UITableViewDelegate>

@end

@implementation SettingTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

#pragma mark - TableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    switch (section) {
        case 0:
            return 5;
            break;
        case 1:
            return 1;
            break;
        case 2:
            return 1;
            break;
    }
    return 0;
}

- (SettingCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString * reuseID = @"settingCell";
    
    SettingCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseID];
    
    if (!cell) {
        cell = [[SettingCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseID];
    }
    
    switch (indexPath.section) {
        case 0: {
            switch (indexPath.row) {
                case 0:
                    cell.textLabel.text = @"使用说明";
                    cell.settingText.hidden = YES;
                    break;
                case 1:
                    cell.textLabel.text = @"计费标准";
                    cell.settingText.hidden = YES;
                    break;
                case 2:
                    cell.textLabel.text = @"意见反馈";
                    cell.settingText.hidden = YES;
                    break;
                case 3:
                    cell.textLabel.text = @"关于我们";
                    cell.settingText.hidden = YES;
                    break;
                case 4:
                    cell.textLabel.text = @"法律条款";
                    cell.settingText.hidden = YES;
                    break;
                
            }
        }
            break;
            
        case 1: {
            cell.textLabel.text = @"清除缓存";
            cell.settingText.hidden = YES;
        }
            break;
        case 2: {
            cell.settingText.text = @"退出登录";
            cell.settingText.textColor = [UIColor redColor];
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
            break;
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    switch (indexPath.section) {
        case 0: {
            switch (indexPath.row) {
                case 0: {
                    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
                    InstructionViewController *ivc = [sb instantiateViewControllerWithIdentifier:@"instructionVC"];
                    [self.navigationController pushViewController:ivc animated:YES];
                }
                    break;
                case 1: {
                    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
                    UIViewController *ivc = [sb instantiateViewControllerWithIdentifier:@"priceVC"];
                    [self.navigationController pushViewController:ivc animated:YES];
                }
                    break;
                case 2: {
                    LCUserFeedbackAgent *agent = [LCUserFeedbackAgent sharedInstance];
                    [agent showConversations:self title:nil contact:nil];
                    
//                    LCUserFeedbackViewController *feedbackViewController = [[LCUserFeedbackViewController alloc] init];
//                    feedbackViewController.navigationBarStyle = LCUserFeedbackNavigationBarStyleNone;
//                    feedbackViewController.contactHeaderHidden = YES;
//                    feedbackViewController.feedbackTitle = [AVUser currentUser].username;
//                    JTBaseNaviController *navigationController = [[JTBaseNaviController alloc] initWithRootViewController:feedbackViewController];
//                    [self presentViewController:navigationController animated:YES completion: ^{
//                    }];
                }
                    break;
                case 3: {
                    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
                    AboutUsViewController *ivc = [sb instantiateViewControllerWithIdentifier:@"aboutusVC"];
                    [self.navigationController pushViewController:ivc animated:YES];
                }
                    break;
                case 4: {
                    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
                    JTProvisionViewController *vc = [sb instantiateViewControllerWithIdentifier:@"provision"];
                    [self.navigationController pushViewController:vc animated:YES];
                }
                    break;
            }
        }
            break;
            
        case 1: {
            [self clearCacheClick];
        }
            break;

        case 2: {
            [self logOutClick];
        }
            break;
    }
}

#pragma mark - 设置页面操作

- (void)clearCacheClick {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"确定要清除缓存吗？" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        SDWebImageManager *mgr = [SDWebImageManager sharedManager];
        [mgr.imageCache clearDisk];
        [AVQuery clearAllCachedResults];
        [SVProgressHUD showSuccessWithStatus:@"清除成功"];
    }];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alert addAction:sure];
    [alert addAction:cancel];
    [self presentViewController:alert animated:YES completion:nil];
}

- (void)logOutClick {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"警告" message:@"确定要退出登录吗？" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [JTUser logOut];
        [SVProgressHUD showSuccessWithStatus:@"成功退出"];
        [self.navigationController popToRootViewControllerAnimated:YES];
    }];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alert addAction:sure];
    [alert addAction:cancel];
    [self presentViewController:alert animated:YES completion:nil];
}

@end
